import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import MainLayout from '@/layouts/MainLayout.vue'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: { title: '注册' }
  },
  // 使用统一布局的路由
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页', requireAuth: true }
      },
      {
        path: 'skill',
        name: 'SkillList',
        component: () => import('@/views/skill/SkillList.vue'),
        meta: { title: '技能广场', requireAuth: true }
      },
      {
        path: 'skill/detail/:id',
        name: 'SkillDetail',
        component: () => import('@/views/skill/SkillDetail.vue'),
        meta: { title: '技能详情', requireAuth: true }
      },
      {
        path: 'skill/publish',
        name: 'SkillPublish',
        component: () => import('@/views/skill/SkillPublish.vue'),
        meta: { title: '发布技能', requireAuth: true }
      },
      {
        path: 'appointment',
        name: 'Appointment',
        component: () => import('@/views/appointment/AppointmentList.vue'),
        meta: { title: '我的预约', requireAuth: true }
      },
      {
        path: 'appointment/detail/:id',
        name: 'AppointmentDetail',
        component: () => import('@/views/appointment/AppointmentDetail.vue'),
        meta: { title: '预约详情', requireAuth: true }
      },
      {
        path: 'chat',
        name: 'Chat',
        component: () => import('@/views/Chat.vue'),
        meta: { title: '聊天', requireAuth: true }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile.vue'),
        meta: { title: '个人中心', requireAuth: true }
      },
      {
        path: 'admin',
        name: 'Admin',
        component: () => import('@/views/Admin.vue'),
        meta: { title: '管理后台', requireAuth: true, requireAdmin: true }
      },
      {
        path: 'notice',
        name: 'Notice',
        component: () => import('@/views/Notice.vue'),
        meta: { title: '系统公告', requireAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  document.title = to.meta.title ? `${to.meta.title} - TimeShare` : 'TimeShare'
  
  const userStore = useUserStore()
  const token = userStore.token
  
  // 需要登录的页面
  if (to.meta.requireAuth && !token) {
    next('/login')
    return
  }
  
  // 已登录用户访问登录/注册页
  if ((to.path === '/login' || to.path === '/register') && token) {
    next('/home')
    return
  }
  
  // 需要管理员权限的页面
  if (to.meta.requireAdmin) {
    // 如果用户信息还未加载，先加载
    if (!userStore.userInfo) {
      await userStore.fetchUserInfo()
    }
    
    // 检查是否为管理员
    if (userStore.userInfo?.role !== 1) {
      ElMessage.error('无权访问管理后台')
      next('/home')
      return
    }
  }
  
  next()
})

export default router

